
import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

///通知提醒
class NoticePage extends StatefulWidget {
  const NoticePage({Key? key}) : super(key: key);

  @override
  State<NoticePage> createState() => _NoticePageState();
}

class _NoticePageState extends State<NoticePage> {

  int mode = 0;

  var editingController = TextEditingController();

  AppAllValueController controller = Get.find();

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg) {
      Map maps = arg;
      switch(maps[DeviceKey.DataType] as String){

      }
    });
  }


  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("通知提醒".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          GridView(
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3,childAspectRatio: 4/1),
            shrinkWrap: true,
            children: [
              buildNoticeWidget(0,"Call"),
              buildNoticeWidget(255,"Stop_call"),
              buildNoticeWidget(1,"Sms"),
              buildNoticeWidget(2,"WeChat"),
              buildNoticeWidget(3,"Facebook"),
              buildNoticeWidget(4,"Instagram"),
              buildNoticeWidget(5,"Skype"),
              buildNoticeWidget(6,"Telegram"),
              buildNoticeWidget(7,"Twitter"),
              buildNoticeWidget(8,"vkclient"),
              buildNoticeWidget(9,"WhatApp"),
              buildNoticeWidget(10,"QQ"),
              buildNoticeWidget(11,"In"),
    ],
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: [
              Container(
                width: 300,
                child: TextField(
                  controller: editingController,
                  decoration: InputDecoration(
                    hintText: '请输入内容'.tr
                  ),
                  textAlign: TextAlign.center,
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.done,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[a-zA-Z]')),//只允许输入英文
                  ],
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('发送'.tr),
                  onPressed: (){
                    if(editingController.text.isEmpty)return;
                    var notifier = Notifier(type: mode,info: editingController.text);
                    controller.writeData(BleSDK.setNotifyData(notifier));
                  },
                ),
              )),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildNoticeWidget(int index,String text){
    return GestureDetector(
      child: Container(
        color: Colors.white,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Radio(
              value: mode,
              activeColor: Colors.blue, //选中时的颜色
              onChanged:(value){
                if(value != null){
                  setState(() {
                    mode = index;
                  });
                }
              }, groupValue: index,
            ),
            Text(text)
          ],
        ),
      ),
      onTap: (){
        setState(() {
          mode = index;
        });
      },
    );
  }
}
